// component.cpp
#include "component.h"
#include <QFile>

Component::Component(const QString &name, const QString &iconPath, Type type, const QString &description) :
    m_name(name), m_type(type), m_description(description)
{
    if (!iconPath.isEmpty() && QFile::exists(iconPath)) {
        m_icon = QIcon(iconPath);
    } else {
        // 使用默认图标或生成彩色矩形作为占位符
        QPixmap pixmap(64, 64);
        pixmap.fill(QColor(qrand() % 256, qrand() % 256, qrand() % 256));
        m_icon = QIcon(pixmap);
    }
}

Component::Component(const Component &other) :
    m_name(other.m_name),
    m_icon(other.m_icon),
    m_type(other.m_type),
    m_description(other.m_description),
    m_value(other.m_value),
    m_connectionPoints(other.m_connectionPoints)
{
}

QString Component::name() const
{
    return m_name;
}

void Component::setName(const QString &name)
{
    m_name = name;
}

QIcon Component::icon() const
{
    return m_icon;
}

void Component::setIcon(const QString &path)
{
    m_icon = QIcon(path);
}

void Component::setIcon(const QIcon &icon) {
    m_icon = icon;
}

Component::Type Component::type() const
{
    return m_type;
}

QString Component::typeString() const
{
    switch (m_type) {
    case Wire: return "Wire";
    case Connector: return "Connector";
    case Passive: return "Passive Component";
    case Semiconductor: return "Semiconductor";
    case Power: return "Power Source";
    case Measurement: return "Measurement Device";
    case Custom: return "Custom Component";
    default: return "Unknown";
    }
}

void Component::setType(Type type)
{
    m_type = type;
}

QString Component::description() const
{
    return m_description;
}

void Component::setDescription(const QString &description)
{
    m_description = description;
}

QString Component::value() const
{
    return m_value;
}

void Component::setValue(const QString &value)
{
    m_value = value;
}

QList<QPointF> Component::connectionPoints() const
{
    return m_connectionPoints;
}

void Component::addConnectionPoint(const QPointF &point)
{
    m_connectionPoints.append(point);
}
